home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Programming / powerd / examples / Bezier.d < prev    next >
Encoding:
Text File  |  2000-09-29  |  2.6 KB  |  116 lines

  1. // Bezier.d - example of how to generate bezier curves in D
  2. // requires workbench height of atleast ~340 pixels and fpu
  3.  
  4. MODULE    'intuition/intuition',
  5.             'utility/tagitem'
  6.  
  7. PROC main()
  8.     DEF    w:PTR TO Window,class,change=FALSE,code,x,y
  9.     DEF    id=-1,drag:PTR TO xy
  10.     IF w:=OpenWindowTags(NIL,
  11.             WA_InnerWidth,320,
  12.             WA_InnerHeight,320,
  13.             WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE,
  14.             WA_Flags,WFLG_DRAGBAR|WFLG_GIMMEZEROZERO|WFLG_RMBTRAP|WFLG_ACTIVATE|WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_REPORTMOUSE,
  15.             WA_Title,'Bezier''s curve',
  16.             TAG_END)
  17.  
  18.         drag:=[            // draggable points
  19.             -100,-100,
  20.             +100,-100,
  21.             +100,+100,
  22.             -100,+100
  23.             ]:xy
  24.  
  25.         draw    // watch the definition below
  26.  
  27.         EasyRequestArgs(0,[SIZEOF_EasyStruct,0,0,'Drag those points and enjoy :)','Yea']:EasyStruct,0,NIL)
  28.  
  29.         WHILE (class,code:=WaitIMessage(w))<>IDCMP_CLOSEWINDOW
  30.             SELECT class
  31.             CASE IDCMP_MOUSEBUTTONS
  32.                 SELECT code
  33.                 CASE SELECTDOWN
  34.                     x:=w.GZZMouseX-160
  35.                     y:=w.GZZMouseY-160
  36.                     IF (id:=Drag(x,y,drag,4))>=0 THEN change:=TRUE
  37.                 DEFAULT
  38.                     id:=-1
  39.                 ENDSELECT
  40.             CASE IDCMP_MOUSEMOVE
  41.                 IF id>=0
  42.                     x:=w.GZZMouseX-160
  43.                     y:=w.GZZMouseY-160
  44.                     drag[id].x:=x
  45.                     drag[id].y:=y
  46.                     change:=TRUE
  47.                 ENDIF
  48.             ENDSELECT
  49.         NEXTIF change=FALSE
  50.             draw    // watch the definition below
  51.             change:=FALSE
  52.         ENDWHILE
  53.  
  54.         CloseWindow(w)
  55.     ENDIF
  56.  
  57.     SUB draw        // this is new feature from v0.16 of PowerD :)
  58.         SetRast(w.RPort,0)
  59.         DrawCV(w.RPort,drag,4)
  60.         Bezier(w.RPort,drag,100)
  61.     ENDSUB
  62.  
  63. ENDPROC
  64.  
  65. OBJECT xy
  66.     x/y:L
  67.  
  68. // Bezier() - creates a curve from a list of 4 points
  69.  
  70. PROC Bezier(rp,p:PTR TO xy,steps)
  71.     DEFF    delta,t,x,y
  72.     DEFF    b1,b2,b3,b4
  73.     DEF    i
  74.     delta:=1.0/steps
  75.     SetAPen(rp,2)
  76.     Move(rp,p[0].x+160,p[0].y+160)
  77.     FOR i:=0 TO steps
  78.         t:=delta*i
  79.         b1:=B1(t)
  80.         b2:=B2(t)
  81.         b3:=B3(t)
  82.         b4:=B4(t)
  83.         x:=b1*p[0].x+b2*p[1].x+b3*p[2].x+b4*p[3].x        // parametrical representations for x and y coords
  84.         y:=b1*p[0].y+b2*p[1].y+b3*p[2].y+b4*p[3].y
  85.         Draw(rp,x+160,y+160)
  86.     ENDFOR
  87. ENDPROC
  88.  
  89. // Cubical (Berstein's) polynoms
  90. PROC B1(t:F)(F) IS (1.0-t)*(1.0-t)*(1.0-t)
  91. PROC B2(t:F)(F) IS 3.0*t*(1.0-t)*(1.0-t)
  92. PROC B3(t:F)(F) IS 3.0*t*t*(1.0-t)
  93. PROC B4(t:F)(F) IS t*t*t
  94.  
  95. PROC DrawCV(rp,p:PTR TO xy,count)
  96.     DEF    i
  97.     SetAPen(rp,3)
  98.     RectFill(rp,p.x-2+160,p.y-2+160,p.x+2+160,p.y+2+160)
  99.     FOR i:=0 TO count-2
  100.         SetAPen(rp,1)
  101.         Move(rp,p[i].x+160,p[i].y+160)
  102.         Draw(rp,p[i+1].x+160,p[i+1].y+160)
  103.         SetAPen(rp,3)
  104.         RectFill(rp,p[i+1].x-2+160,p[i+1].y-2+160,p[i+1].x+2+160,p[i+1].y+2+160)
  105.     ENDFOR
  106. ENDPROC
  107.  
  108. PROC Drag(x,y,drag:PTR TO xy,count)(L)
  109.     DEF    id
  110.     FOR id:=0 TO count-1
  111.         IF x>=drag[id].x-2 AND x<=drag[id].x+2 AND y>=drag[id].y-2 AND y<=drag[id].y+2 THEN RETURN id
  112.     ENDFOR
  113. ENDPROC -1
  114.  
  115. // MarK 29/9/2000
  116.